// var user = document.getElementById("user");
// var pass = document.getElementById("pass");
// var tel = document.getElementById("tel");
// var email = document.getElementById("email");
// var btn = document.getElementById("btn");

// var url = "http://api.icodeilife.cn:81/user";
// var u = user.value;
// var p = pass.value;
// var t = tel.value;
// var e = email.value;
class Register{
    constructor(){
        this.user = document.getElementById("user");
        this.pass = document.getElementById("pass");
        this.tel = document.getElementById("tel");
        this.email = document.getElementById("email");
        this.btn = document.getElementById("btn");
        this.msg = document.querySelector(".msg");
        this.url = "http://api.icodeilife.cn:81/user";

        this.regU = /^[a-zA-Z0-9_-]{4,16}$/;
        this.regP = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
        this.regT = /^1[3-9]\d{9}$/;
        this.regE = /^[0-9a-zA-Z]{4,9}@[\da-zA-Z]{2,9}\.[a-zA-Z]{2,4}$/;
        this.addEvent()
    }
    addEvent(){
        var that = this;
        var a = 0;
        var b = 0;
        var c = 0;
        var d = 0;
        this.user.onblur = function () {
            if(that.regU.test(that.user.value)){
                a = 1;
                that.pass.onblur = function () {
                    if(that.regP.test(that.pass.value)){
                        b = 1;
                        that.tel.onblur = function () {
                            if(that.regT.test(that.tel.value)){
                                c = 1;
                                that.email.onblur = function () {
                                    if(that.regE.test(that.email.value)){
                                        d = 1;
                                    }else{
                                        alert("请输入正确的邮箱")
                                        d = 0
                                    }    
                                }
                            }else{
                                alert("请输入正确的手机号");
                                c = 0
                            }
                        }
                    }else{
                        alert("请输入正确的密码");
                        b = 0
                    }
                }
            }else{
                alert("请输入正确的用户名");
                a = 0;
            }
        }
        


        this.btn.onclick = function () {
            if(a+b+c+d == 4){
                that.u = user.value;
                that.p = pass.value;
                that.t = tel.value;
                that.e = email.value;
                that.load();
            }else{
                alert("请输入正确的信息")
                return
            }
        }
    }
    load(){
        ajax({
            url:this.url,
            data:{      
                type: "register",
                user: this.u,
                pass: this.p,
                tel: this.t,
                email: this.e
            }
        }).then((res)=>{
            // console.log(res);
            this.res = JSON.parse(res);
            this.display();
        })
    }
    display(){
        if(this.res.code == 1){
            this.msg.innerHTML = this.res.msg + "，3秒后到登录页面";
            setTimeout(()=>{
                location.href = "login.html";
            },3000);
        }else{
            this.msg.innerHTML = this.res.msg;
        }
    }
}
new Register;








